package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.Fee;
import cn.net.shuxing.yifu.common.constant.DealScene;
import cn.net.shuxing.yifu.common.constant.DealType;
import cn.net.shuxing.yifu.common.constant.RaitoList;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.mapper.FeeMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

@Service
public class FeeService extends BaseService<Fee, FeeMapper> {

    @Autowired
    private UserCapitalService userCapitalService;

    public void updateById(Integer id, BigDecimal raito, BigDecimal count) {
        Fee fee = getByPrimaryKey(id);
        fee.setFeeRatio(raito);
        fee.setBaseDealCount(count);
        update(fee);
    }

    public List<Fee> getByCoinTypeId(Integer coinTypeId) {
        CommonExample commonExample = new CommonExample(Fee.class);
        commonExample.equals("coinTypeId", coinTypeId);
        List<Fee> fees = getsByExample(commonExample);
        return fees;
    }

    public Fee getFee(Integer userId, Integer coinTypeId, Integer dealScene) {
        Fee fee = new Fee();
        fee.clearCreateTimeAndUpdateTime();
        fee.setUserId(userId);
        fee.setCoinTypeId(coinTypeId);
        fee.setDealScene(dealScene);
        fee = get(fee);
        if (fee == null || fee.getFeeRatio() == null || fee.getFeeRatio().compareTo(new BigDecimal(0)) <= 0)
            fee = getDefaultFee(coinTypeId, dealScene);
        return fee;
    }

    public void insert(Integer coinTypeId, Integer dealScene, BigDecimal feeRetio, BigDecimal baseCount) {
        Fee fee = new Fee();
        fee.setDealScene(dealScene);
        fee.setCoinTypeId(coinTypeId);
        fee.setFeeRatio(feeRetio);
        fee.setBaseDealCount(baseCount);
        fee.setUserId(0);
        insert(fee);
    }

    public Fee getDefaultFee(Integer coinTypeId, Integer dealScene) {
        Fee fee = new Fee();
        fee.clearCreateTimeAndUpdateTime();
        fee.setCoinTypeId(coinTypeId);
        fee.setDealScene(dealScene);
        fee = get(fee);
        return fee;
    }

    /**
     * 扣除手续费
     *
     * @param userId
     * @param coinTypeId
     * @param dealScene   交易场景
     * @param dealCount   交易活币数量
     * @param singlePrice 单价
     */
    public void reduceFee(Integer userId, Integer coinTypeId, Integer dealScene, BigDecimal dealCount, BigDecimal singlePrice, int type) {
        Fee fee = getFee(userId, coinTypeId, dealScene);

        if (dealCount.compareTo(fee.getBaseDealCount()) < 0) {
        } else {
            BigDecimal feeRatio = fee.getFeeRatio();
            BigDecimal baseDealCount = fee.getBaseDealCount();
            BigDecimal feeCount = (dealCount.subtract(baseDealCount)).multiply(feeRatio);
            userCapitalService.reduceAliveCoin(userId, coinTypeId, feeCount, type);
        }
    }

    public BigDecimal reduceFeeRetureFeeCount(Integer userId, Integer coinTypeId, Integer dealScene, BigDecimal dealCount, BigDecimal singlePrice, int type) {
        Fee fee = getFee(userId, coinTypeId, dealScene);
        BigDecimal feeCount = new BigDecimal(0);
        if (dealCount.compareTo(fee.getBaseDealCount()) < 0) {
        } else {
            BigDecimal feeRatio = fee.getFeeRatio();
            BigDecimal baseDealCount = fee.getBaseDealCount();
            feeCount = (dealCount.subtract(baseDealCount)).multiply(feeRatio);
            userCapitalService.reduceAliveCoin(userId, coinTypeId, feeCount, type);
        }
        return feeCount;
    }

    public void reduceFee(Integer userId, Integer coinTypeId, Integer dealScene, BigDecimal dealCount, int type) {
        Fee fee = getFee(userId, coinTypeId, dealScene);
        if (dealCount.compareTo(fee.getBaseDealCount()) < 0) {
            return;
        } else {
            BigDecimal feeRatio = fee.getFeeRatio();
            BigDecimal baseDealCount = fee.getBaseDealCount();
            BigDecimal feeCount = (dealCount.subtract(baseDealCount)).multiply(feeRatio);
            userCapitalService.reduceAliveCoin(userId, coinTypeId, feeCount, type);
        }
    }
    public void reduceFeeNoBase(Integer userId, Integer coinTypeId, Integer dealScene, BigDecimal dealCount, int type) {
        Fee fee = getFee(userId, coinTypeId, dealScene);
        if (dealCount.compareTo(fee.getBaseDealCount()) < 0) {
            return;
        } else {
            BigDecimal feeRatio = fee.getFeeRatio();
            BigDecimal feeCount = dealCount.multiply(feeRatio);
            userCapitalService.reduceAliveCoin(userId, coinTypeId, feeCount, type);
        }
    }

    public void setAll(List<RaitoList> list) {
        for (RaitoList raitoList : list) {
            Integer id = raitoList.getId();
            BigDecimal raito = raitoList.getRaito();
            BigDecimal count = raitoList.getCount();
            Fee fee = getByPrimaryKey(id);
            fee.setBaseDealCount(count);
            fee.setFeeRatio(raito);
            update(fee);
        }

    }

    @Override
    protected void complete(Fee bean) {
        Integer dealScene = bean.getDealScene();
//        0在线挂买、1在线挂卖、2股东股东、3股东激活新会员、4会员转会员、5会员转商家、6商家转商家、7商家转会员、8系统回购服务费、9系统回购手续费、10用户购买、11解冻回购手续费
        if (dealScene == 0)
            bean.setDealSceneName("挂买手续费");
        if (dealScene == 1)
            bean.setDealSceneName("挂卖手续费");
        if (dealScene == 2)
            bean.setDealSceneName("股东转股东手续费");
        if (dealScene == 3)
            bean.setDealSceneName("股东激活新会员手续费");
        if (dealScene == 4)
            bean.setDealSceneName("会员转会员手续费");
        if (dealScene == 5)
            bean.setDealSceneName("会员转商家手续费");
        if (dealScene == 6)
            bean.setDealSceneName("商家转商家手续费");
        if (dealScene == 7)
            bean.setDealSceneName("商家转会员手续费");
        if (dealScene == 8)
            bean.setDealSceneName("系统回购服务费");
        if (dealScene == 9)
            bean.setDealSceneName("系统回购手续费");
        if (dealScene == 10)
            bean.setDealSceneName("用户购买手续费");
        if (dealScene == 11)
            bean.setDealSceneName("解冻回购手续费");
    }
}
